<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="post" name="entryForm" id="entryForm">
            <p><label for="lblCategory">Select Category</label></p>
            <p><select name="selCategory" id="category"></p>
                <option value="Education">Education</option>
                <option value="News">News</option>
                <option value="Entertainment">Entertainment</option>
                <option value="Other">Other</option>
            </select></p>
            <p><label for="lblLinkName">Enter Name of Link</label></p>
            <p><input type="text" name="txtLinkName" id="txtLinkName" /></p>
            <p><label for="lblURLName">Enter Name of URL</label></p>
            <p><input type="text" name="txtURLName" id="txtURLName" /></p>     
            <br><br><br><br>
            <p><input type="submit" name="submit" value="Submit New Entry" /></p>
            <p><input type="submit" name="back" value="Go Back to Main Page" /></p>
        </form>
        
        <?php
        //checking for form submission
        $urlDoc = 'urlList.txt';
        $urlFile = fopen($urlDoc, 'a+');
            
        if (isset ($_POST['submit']))
        {
            $linkName = trim($_POST['txtLinkName']);
            $category = $_POST['selCategory'];
            $url = trim($_POST['txtURLName']);
            $dateAdded = trim(date("d-m-y"));
            $counter = 0;
            
            if (filesize($urlDoc) == 0)
            {
                //writing to the file
                fwrite($urlFile, "$linkName, $category, $url, $dateAdded, $counter\r\n");
                echo 'New Entry has been added.';
            }
            else
            {
                //writing to the file
                fwrite($urlFile, "$linkName, $category, $url, $dateAdded, $counter\r\n");
                echo 'New Entry has been added.';
            }
            fclose($urlFile);
        }
        
        
        if (isset ($_POST['back']))
        {
            header("Location: main.php");
        }
    ?>
    </body>
</html>
